package com.zhuiyi.zhxy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhuiyi.zhxy.pojo.Teacher;
import com.zhuiyi.zhxy.service.TeacherService;
import com.zhuiyi.zhxy.util.MD5;
import com.zhuiyi.zhxy.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:
 * @Author: zhuiyi
 * @CreatTime: 2023/1/22 21:05
 **/
@Api(tags = "老师管理器")
@RestController
@RequestMapping("/sms/teacherController")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    //sms/teacherController/getTeachers/1/3?name=11&clazzName=一年二班
    //Request Method: GET
    @ApiOperation("分页带条件查询老师信息")
    @GetMapping("/getTeachers/{pageNo}/{pageSize}")
    public Result getTeachers(
            @ApiParam("页码数") @PathVariable("pageNo") Integer pageNo,
            @ApiParam("页大小") @PathVariable("pageSize") Integer pageSize,
            @ApiParam("JSON格式的teacher对象类型的查询条件") Teacher teacher){
        Page<Teacher> page = new Page<>(pageNo,pageSize);

        IPage<Teacher> iPage = teacherService.getTeachers(page,teacher);
        return Result.ok(iPage);
    }
    /**
     * Request URL: http://localhost:9001/sms/teacherController/saveOrUpdateTeacher
     * Request Method: POST
     */
    @ApiOperation("新增或保存教师")
    @PostMapping("/saveOrUpdateTeacher")
    public Result saveOrUpdateTeacher(@ApiParam("JSON格式的老师对象") @RequestBody Teacher teacher){

        if (teacher.getId() == null || teacher.getId() == 0) {
            teacher.setPassword(MD5.encrypt(teacher.getPassword()));
        }
        teacherService.saveOrUpdate(teacher);
        return Result.ok();
    }

    /**
     * Request URL: http://localhost:9001/sms/teacherController/deleteTeacher
     * Request Method: DELETE
     */
    @DeleteMapping("/deleteTeacher")
    public Result deleteTeacher(@ApiParam("要删除的id数组") @RequestBody List<Integer> ids){
        teacherService.removeByIds(ids);
        return Result.ok();
    }
}
